USE [QLLVANTAI]
GO

/****** Object:  StoredProcedure [dbo].[sp_XoaLichVanChuyen]    Script Date: 09/26/2013 00:55:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE proc [dbo].[sp_XoaLichVanChuyen] 
		@mavanchuyen int, @maxe int
		
as

begin tran

	--set tran isolation level read uncommitted
	set tran isolation level SERIALIZABLE
	-- xoa trong bang dang ky
	--if exists (select * from DangKy where Ma_VanChuyen = @mavanchuyen and Ma_Xe = @maxe)
	--begin
		delete from DangKy where Ma_VanChuyen = @mavanchuyen and Ma_Xe = @maxe
	--end
	
	-- xoa trong bang ChiTietLich
		delete from ChiTietLich where Ma_VanChuyen = @mavanchuyen and Ma_Xe = @maxe
		
	-- xoa trong bang LichVanChuyen
	
		if not exists( select * from ChiTietLich CTL, LichVanChuyen LVC where CTL.Ma_VanChuyen = LVC.Ma_VanChuyen and CTL.Ma_VanChuyen = @mavanchuyen and LVC.Ma_VanChuyen = @mavanchuyen)
		begin
			delete from LichVanChuyen where Ma_VanChuyen = @mavanchuyen
		end 
	
	commit tran
GO

